package com.flyee.controller.tools.cartoon.control
{
	import com.flyee.controller.tools.cartoon.model.ConfigObj;
	import com.flyee.controller.tools.cartoon.model.CreateAppearanceModel;
	import com.flyee.events.AppearanceEvent;
	import com.flyee.utils.load.StackLoader;
	import com.flyee.utils.load.contorl.LoaderManager;
	import com.flyee.utils.load.model.LoaderModel;
	import com.flyee.utils.load.processor.LoadLevelType;
	import com.flyee.utils.load.processor.LoadReturnType;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.ProgressEvent;
	import flash.utils.Dictionary;
	
	import mx.controls.Alert;
	import mx.controls.ProgressBar;
	import mx.controls.ProgressBarLabelPlacement;
	import mx.controls.ProgressBarMode;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	import mx.managers.PopUpManagerChildList;
	
	public class AppearanceLoadManager
	{
		
		private var needLoadList:Array = new Array();
		private var loaderList:Array = new Array();
		private var progressWindow:ProgressBar = null;
		
		private static var instance:AppearanceLoadManager = null;
		public function AppearanceLoadManager()
		{
			if (instance)
			{
				throw new Error("singleton error!!!");
			}
			instance = this;
		}
		public static function getInstance():AppearanceLoadManager
		{

			if (instance == null)
			{
				instance = new AppearanceLoadManager();
			}
			return instance;
		}
		private var loaderDic:Dictionary = new Dictionary();
		public function regist(t:String,returnFun:Function,id:String):void
		{
			t = ConfigObj.absoluteURL + t;
			needLoadList.push(t);
			this.loaderDic[t] = returnFun;
			var loader:StackLoader = new StackLoader();
			loader.load(t,LoadLevelType.LV_2,this.progressHandle,this.completeHandle,LoadReturnType.RETURN_LOADER,id);
		}
		private function progressHandle(e:ProgressEvent):void
		{
//			if(this.progressWindow != null)
//			{
//				this.progressWindow.setProgress( e.bytesLoaded,e.bytesTotal);
//				this.progressWindow.label = "正在加载装备共享库"+this.needLoadList.length +"   "+Math.round(this.progressWindow.percentComplete)+"%"
//			}
//			else
//			{
//				this.progressWindow = new ProgressBar();
//				this.progressWindow.setProgress( e.bytesLoaded,e.bytesTotal);
//				this.progressWindow.mode = ProgressBarMode.MANUAL;
//				this.progressWindow.maximum = 100;
//				this.progressWindow.minimum = 0;
//				this.progressWindow.label = "正在加载装备共享库"+this.needLoadList.length +"   "+Math.round(this.progressWindow.percentComplete)+"%"
//				this.progressWindow.labelPlacement = ProgressBarLabelPlacement.TOP;
//				this.progressWindow.width = 200;
//				PopUpManager.addPopUp(this.progressWindow,Application.application as DisplayObjectContainer,true,PopUpManagerChildList.POPUP);
//				PopUpManager.centerPopUp(this.progressWindow);
//			}
			
		}
		private function completeHandle(loader:LoaderModel):void
		{
			if(this.progressWindow)
			{
				PopUpManager.removePopUp(this.progressWindow);
				this.progressWindow = null;
			}
			if(loader)
			{
				var tempLength:uint = this.needLoadList.length;
				for(var i:int = 0; i< tempLength;i++)
				{
					if(loader.loadURL == this.needLoadList[i])
					{
						loaderList.push(loader);
					}
				}
				//loader.loadReturnFun(loader);
				var tempF:Function = this.loaderDic[loader.loadURL] as Function;
				if(tempF != null)
				{
					tempF(loader)
				}
			}
			else
			{
				Alert.show("装备共享库加载失败，请检查网络。\n "+loader.memo.toString(),"提示")
				this.clear();
				return;
			}
			var temp:CreateAppearanceModel = AppearanceMananger.getInstance().getAppCartoon(loader.memo.toString());
			if(temp)
			{
				temp.appearanceCartoon.dispath(new AppearanceEvent(AppearanceEvent.COMPLETE_SHAREDEPORT_APPEARANCE,{shareDeport:loader.loadURL,appearanceCartoon:temp.appearanceCartoon}));
			}
			else
			{
				Alert.show("装备共享库加载失败，请检查网络。\n "+loader.loadURL,"提示")
				this.clear();
				return;
			}
		}
		/**
		 * 
		 * @param loaderURL
		 * @param className
		 * @return 
		 * 
		 */		
		public function getClass(loaderURL:String,className:String):Class
		{
			var temp:LoaderModel = this.getLoader(loaderURL);
			return temp.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
		}
		/**
		 * 
		 * @param loaderURL
		 * @return 
		 * 
		 */		
		public function getLoader(loaderURL:String):LoaderModel
		{
			loaderURL = ConfigObj.absoluteURL + loaderURL;
			var tempLength:uint = this.loaderList.length;
			for(var i:uint = 0; i< tempLength;i++)
			{
				var temp:LoaderModel = this.loaderList[i];
				if(temp.loadURL == loaderURL)
				{
					return temp;
				}
			}
			return null;
		}
		/**
		 * 
		 * 
		 */		
		public function clear():void
		{
			if(this.progressWindow)
			{
				PopUpManager.removePopUp(this.progressWindow);
				this.progressWindow = null;
			}
			var tempLength:uint = this.loaderList.length;
			for(var i:int = 0; i< tempLength; i++)
			{
				var temp:LoaderModel = this.loaderList[i];
				LoaderManager.getInstance().removeLoader(temp.loadID);
				if(temp)
				{
					temp.clear();
					temp = null;
				}
			}
			this.loaderList.splice(0);
			this.needLoadList.splice(0);
		}
	}
}